//
//  OfferToSellViewController.m
//  1243
//
//  Created by 洛水寒 on 16/5/24.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "OfferToSellViewController.h"
#import "NetWorkTools.h"//网络请求

@interface OfferToSellViewController ()

@property (weak, nonatomic) IBOutlet UITextField *saleNum;//出售数量
@property (weak, nonatomic) IBOutlet UITextField *salePrice;//出售价格
@property (weak, nonatomic) IBOutlet UILabel *marketPrice;//市场价
@property (weak, nonatomic) IBOutlet UILabel *ownNum;//拥有数量

@property (weak, nonatomic) IBOutlet UILabel *totalPrice;//总计金额

@end

@implementation OfferToSellViewController
{
    NSNotificationCenter *_nc;//通知中心
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"发布出售";
    
    
    //网络请求
    [self getMarketPrice];
    
    
    
    if ([_ownNumString isEqualToString: @"(null)"]) {
        _ownNum.text = @"0";
    }else{
        _ownNum.text = _ownNumString;
    }
    
    _salePrice.keyboardType = UIKeyboardTypeDecimalPad;
    
    
    _nc = [NSNotificationCenter defaultCenter];
    //注册观察者 通知的名字一致
    [_nc addObserver:self selector:@selector(saleNumDidEndEdit) name:UITextFieldTextDidChangeNotification object:nil];
    [_nc addObserver:self selector:@selector(salePricedidEndEdit) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark === 通知方法 ===
- (void)saleNumDidEndEdit{
    if (![_saleNum.text isEqualToString:@""] && ![_salePrice.text isEqualToString:@""]) {
        _totalPrice.text = [NSString stringWithFormat:@"%.2f", [_saleNum.text floatValue] * [_salePrice.text floatValue]];
    }
}
- (void)salePricedidEndEdit{
    if (![_saleNum.text isEqualToString:@""] && ![_salePrice.text isEqualToString:@""]) {
        _totalPrice.text = [NSString stringWithFormat:@"%.2f", [_saleNum.text floatValue] * [_salePrice.text floatValue]];
    }
}


#pragma mark === 网络请求 ===
- (void)getMarketPrice{
    NSDictionary *params = @{
                             };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params AndMenthodName:@"getPriceAndQty" completeBlock:^(id object) {
        
        
        NSString *str = object[@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:str];
        
        if (arr == nil) {
            _marketPrice.text = @"";
        }else{
            _marketPrice.text = [NSString stringWithFormat:@"%@", arr[0][@"MarketPrice"]];
        }
    }];
}
- (void)getsaleInfo{
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *TradeUserId = [dict objectForKey:@"Number"];
    NSString *TradeQty = _saleNum.text;
    NSString *TradePrice = _salePrice.text;
    NSString *PayMoney = _totalPrice.text;
    NSString *MarketPrice = _marketPrice.text;
    
    
    
    
    NSDictionary *params = @{
                              //
                              @"TradeUserId":TradeUserId,
                              @"TradeQty":TradeQty,
                              @"TradePrice":TradePrice,
                              @"PayMoney":PayMoney,
                              @"MarketPrice":MarketPrice,
                              };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params AndMenthodName:@"PreSellTicket" completeBlock:^(id object) {
        
        if ([[NSString stringWithFormat:@"%@",object[@"Id"]] isEqualToString:@"0"]) {
            self.saleNum.text = @"";
            self.salePrice.text = @"";
            self.totalPrice.text=  @"";
            [self getMarketPrice];
        }

        //提示框
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:object[@"Message"] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        NSLog(@"%@", object[@"Message"]);
        [message show];
        
           }];
}




- (IBAction)offerToSale:(id)sender {
    //网络请求
    [self getsaleInfo];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
